package com.best.onegous.details;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;

import com.best.onegous.BaseActivity;
import com.best.onegous.R;
import com.best.onegous.Adapter.IndianarecordsAdapter;
import com.best.onegous.Adapter.JieXiaoAdapter;
import com.best.onegous.Refresh.XListView;
import com.best.onegous.Refresh.XListView.IXListViewListener;
import com.best.onegous.agreement.ApiInterface;
import com.best.onegous.http.BusinessResponse;
import com.best.onegous.http.HttpClients;
import com.loopj.android.http.RequestParams;

public class Indianarecords extends BaseActivity implements OnClickListener,IXListViewListener,BusinessResponse{
	private XListView mListView;
	private List<JSONObject> mDatas = new ArrayList<JSONObject>();
	private IndianarecordsAdapter mAdapter;
	private HttpClients httpclient;
	private String codeGoodsID,codePeriod;
	private Intent intent;
	private int page = 1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setThem(R.layout.indianarecords_activity);
		intent = getIntent();
		codeGoodsID = intent.getStringExtra("codeGoodsID");
		codePeriod = intent.getStringExtra("codePeriod");
		
		findViewById(R.id.back).setOnClickListener(this);
		mListView = (XListView) findViewById(R.id.listview);
	
		
		mAdapter = new IndianarecordsAdapter(this, mDatas);
		mListView.setAdapter(mAdapter);
		mListView.setPullRefreshEnable(true);
		mListView.setPullLoadEnable(true);
		mListView.setXListViewListener(this);
		httpclient = new HttpClients(this);
		httpclient.addResponseListener(this);
		loading();
	
	}
	private void loading(){
		 RequestParams params = new RequestParams();
		  params.add("sid",codeGoodsID);
		  params.add("qishu",codePeriod);
		  params.add("p",page+"");
		httpclient.httpclient(ApiInterface.DUOBAOJILU, params, false);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			 finish();
			break;
		
	
		}
	}
	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		page++;
		loading();

	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		page = 1;
		loading();
	
	}
	@Override
	public void OnMessageResponse(String url, JSONObject jo)
			throws JSONException {
		if(1==page){
			mDatas.clear();
			mListView.stopRefresh();
		}else{
			mListView.stopLoadMore();
		}
		
		if (url.equals(ApiInterface.DUOBAOJILU)) {
			
			if (null != jo) {
				
				JSONArray data = jo.getJSONArray("data");
				for(int i = 0,s = data.length();i<s;i++){
					mDatas.add(data.getJSONObject(i));
				}
				
				mAdapter.notifyDataSetChanged();
			}
		}
		
	}  
}
